local blaze = require "blaze"
local app = require "app"
local game = require "club.threecard.hbsl"
local LoopScrollList = require "blaze.ui.widget.LoopScrollList"

local Item = game.ui.element()

local log = game.logging.get("packet_queue")

function Item:on_scroll_cell_update(data, index)
    local player = game.store.gameplay.find_player_by_id(data.sender_id)
    if not player then
        log.warn("没有找到玩家：id"..data.sender_id)
        return
    end

    self.name_txt.text = player.name
    self.amount_txt.text = app.i18n.number(data.amount, "chip3")
end

local M = game.ui.element()

local function refill(self, state)
    self.list:fill(state.red_packets)
end

M.store_watchers = {
    gameplay = {
        ["红包队列变化"] = function(self, store, state, args, init)
            if self.gameobject.activeInHierarchy then
                refill(self, state)
            else
                self.dirty = true
            end
        end
    }
}

function M:ctor()
    self.res = "club.threecard.hbsl/ui/ingame.b:packet_queue"
    self.list = LoopScrollList.new()
    self.list.res = "club.threecard.hbsl/ui/ingame.b:packet_queue_item"
    self.list.create_item = function(_, data, index) return Item.new() end
end

function M:on_injected(bind)
    self.list:attach(self.scroll_rect)
    bind("button.click", self.btn_cancel, self.cancel_ivk)
end

function M:cancel_ivk()
end

function M:show()
    self:super("show")
    if self.dirty then
        refill(self, game.store.gameplay.state)
        self.dirty = false
    end
end

function M:on_load()
    self:super("on_load")
    self.list:load()
    self.list:set_parent(self.list_root, false)
end

function M:on_release()
    self:super("on_release")
    self.list:release()
end

return M